import { NextResponse } from 'next/server'
import mysql from 'mysql2/promise'

export async function GET() {
  try {
    // 直接使用mysql2测试连接
    const connection = await mysql.createConnection({
      host: process.env.MYSQL_HOST,
      port: parseInt(process.env.MYSQL_PORT || '3306'),
      user: process.env.MYSQL_USER,
      password: process.env.MYSQL_PASSWORD,
      database: process.env.MYSQL_DATABASE
    })

    // 测试查询
    const [rows] = await connection.execute('SELECT COUNT(*) as count FROM smoke_user WHERE delete_time = 0')
    const [logs] = await connection.execute('SELECT COUNT(*) as count FROM yh_logs WHERE deleted_at = 0')
    
    await connection.end()

    return NextResponse.json({
      success: true,
      message: '数据库连接成功',
      data: {
        users_count: (rows as any)[0].count,
        logs_count: (logs as any)[0].count,
        connection_info: {
          host: process.env.MYSQL_HOST,
          database: process.env.MYSQL_DATABASE
        }
      }
    })
  } catch (error) {
    console.error('数据库连接错误:', error)
    return NextResponse.json({
      success: false,
      error: error instanceof Error ? error.message : '未知错误',
      message: '数据库连接失败'
    }, { status: 500 })
  }
}